/*
 * Created on 2004-01-06
 */
package org.opencube.oms.meta;

import java.util.Date;

import org.opencube.oms.OMSNode;

import info.fingo.util.Text;

/**
 * @author <a href="mailto:maciek@fingo.pl">FINGO - Maciej Mroczko</a>
 * TODO: comment
 */
public class SimpleAttribute extends OMSNode
{
	/* The scheme of this attribute */
	private Scheme scheme = null;
	/* The datatype of this attribute */
	private Datatype datatype = null;
	/* The default value */
	private String defaultValue = null;
	/* The attribute use flag */
	private char use = DatatypeEnumeration.USE_OPTIONAL;
	
	/**
	 * @param scheme
	 * @param datatype
	 * @param dataType
	 * @param id
	 * @param name
	 * @param creatingDate
	 * @param creater
	 * @param modifyingDate
	 * @param modifier
	 * 
	 * @throws OMSMetaDataException
	 */
	SimpleAttribute( Scheme scheme, Datatype datatype, String defaultValue, char use, String id, String name, Date creatingDate, String creater, Date modifyingDate, String modifier )
	throws OMSMetaDataException
	{
		super( id, name, creatingDate, creater, modifyingDate, modifier );
		this.use = use;
		this.scheme = scheme;
		this.datatype = datatype;
		this.defaultValue = defaultValue;
		scheme.getNamespace().getMetaData().addSimpleAttribute( this );
		
		if( use != DatatypeEnumeration.USE_OPTIONAL && use != DatatypeEnumeration.USE_REQUIRED )
		{
			throw new OMSMetaDataException( "The invalid use flag '" + use + "' for the attribute: name = '" + name + "', id = '" + id + "'" );
		}

		if( datatype == null )
		{
			throw new OMSMetaDataException( "The datatype for the simple attribute cannot be null: name = '" + name + "', id = '" + id + "'"  );
		}
	}

	/**
	 * Returns the attribute datatype
	 * 
	 * @return Datatype - attribute datatype
	 */
	public Datatype getDatatype()
	{
		return datatype;
	}

	/**
	 * Returns the attribute owner scheme
	 * 
	 * @return Scheme - attribute woner scheme
	 */
	public Scheme getScheme()
	{
		return scheme;
	}
	
	/**
	 * Returns the attribute's default value
	 * 
	 * @return The default value
	 */
	public String getDefaultValue()
	{
		return defaultValue;
	}
	
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString()
	{
		return "<simple_attribute name=\"" + name 
					+ "\" id=\"" + id 
					+ "\" datatype=\"" + datatype.getNamespace().getName() + "." + datatype.getName() 
					+ "\" default=\"" + Text.null2string( defaultValue )
				+ "\"/>";
	}
}
